package com.ogawa.chair7808.ui.activity;

import android.os.Handler;

import androidx.fragment.app.Fragment;

import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.LogUtils;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.ogawa.base.Constant.CommmandNum;
import com.ogawa.base.utils.CommonUtil;
import com.ogawa.chair7808.R;
import com.ogawa.chair7808.cache.GlobalCache;
import com.ogawa.chair7808.manager.DataManager;
import com.ogawa.chair7808.manager.TimeBean;
import com.ogawa.chair7808.ui.fragment.AirPressureMassageFragment;
import com.ogawa.chair7808.ui.fragment.FragmentAdapter;
import com.ogawa.chair7808.ui.fragment.MassageTechniqueFragment;
import com.ogawa.chair7808.utils.Activity7808Utils;
import com.ogawa.chair7808.widget.CustomViewPager;

import java.util.ArrayList;
import java.util.List;

import static com.ogawa.base.Constant.Constant.LIVE_EVENT_BUS_KEY_END_TIME;
import static com.ogawa.base.Constant.Constant.LIVE_EVENT_BUS_KEY_UPDATE_TIME;
import static com.ogawa.base.utils.StringUtils.numberToTime;


/**
 * 高级按摩
 */
public class AdvancedMassageActivity7808 extends BaseMqtt7808Activity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener
//        , IAdvancedMassageView7506
//        , CloseAdvance.onTimeFinish, TimeObserver.onTimeUpdataLis
{


    public static final int TAG_ANMOSHOUFA = 0;
    public static final int TAG_QIYAANMO = 1;
    /**
     * 存放内容页面
     */
    private ArrayList<Fragment> contentPage;
    private RadioButton tableft;
    private RadioButton tabright;
//    private ImageView mIvStart;
    private Fragment mFrag;
    //    private ImageView mIvPower;
    private String model;
    private RelativeLayout mRlTop;
    private FragmentAdapter mAdapter;//适配器
    private List<String> mTabs = new ArrayList<>();//存放标签页标题

    //    private AdvancedMassagePressenter7506 mPresenter;
    private MassageTechniqueFragment mFrangment1;
    private AirPressureMassageFragment mFrangment2;
    private CustomViewPager viewPager;
    //    private boolean isFinishShow;
    private boolean isPowerOn;
    private String mMin, mSec;
    private Handler handler;

    @Override
    public void initTitleBar() {
        super.initTitleBar();
        showTitleBar(true);
        titleBar.showAdvance();
        setBackGround(R.mipmap.bg);

    }

    @Override
    public void initView(int layoutId) {
        super.initView(layoutId);
        handler = new Handler();
        initFragment();
        initView();
//        iniTitle();

//        mPresenter = new AdvancedMassagePressenter7506(this);
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                mPresenter.getTimes();
//            }
//        }, 500);
//        CloseAdvance.getInst().registerObserver(this);
//        TimeObserver.getInst().registerObserver(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        GlobalCache.setHandOutAdvancedMassageActivity(true);
    }

    private void initFragment() {
        contentPage = new ArrayList<Fragment>();
        mFrangment1 = new MassageTechniqueFragment();
        mFrangment2 = new AirPressureMassageFragment();

        contentPage.add(mFrangment1);
        contentPage.add(mFrangment2);
        mTabs.add("按摩手法");
        mTabs.add("气压按摩");


    }




    private void initView() {

        model = getIntent().getStringExtra("model");
        viewPager = findViewById(R.id.viewpager);
        viewPager.setOffscreenPageLimit(1);
        mAdapter = new FragmentAdapter(getSupportFragmentManager(), contentPage, mTabs);
        viewPager.setAdapter(mAdapter);//给ViewPager设置适配器
        viewPager.setOnViewPagerScrollAction(new CustomViewPager.setOnViewPagerScrollAction() {
            @Override
            public boolean unAbleScroll() {
                return true;
            }
        });
        tableft = findViewById(R.id.tableft);
        tabright = findViewById(R.id.tabright);

        isPowerOn =GlobalCache.isOpen();
        tableft.setOnCheckedChangeListener(this);
        tableft.setTag(TAG_ANMOSHOUFA);
        tableft.setChecked(true);


        tabright.setOnCheckedChangeListener(this);
        tabright.setTag(TAG_QIYAANMO);
//        mIvStart = findViewById(R.id.iv_switch);
//        mIvStart.setOnClickListener(this);
//        mIvStart.setSelected(DataManager.getDataManager().isPause());
    }


    @Override
    public void onClick(View v) {
        int i = v.getId();

        if (i == R.id.iv_switch) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                DataManager.getDataManager().sendCommond(CommmandNum.PAUSE, "5");
            }

        }

    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        if (isChecked) {
            int i = buttonView.getId();
            if (i == R.id.tableft) {
                viewPager.setCurrentItem(0);
                tableft.setBackgroundResource(R.mipmap.tab_letf);
                tabright.setBackgroundResource(0);

            } else if (i == R.id.tabright) {
                tabright.setBackgroundResource(R.mipmap.tab_letf);
                tableft.setBackgroundResource(0);
                viewPager.setCurrentItem(1);

            } else {
            }

        }
    }


    @Override
    public int setLayoutId() {
        return R.layout.activity_advanced_massage_7808;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void changeViewByMqttMessage() {
        super.changeViewByMqttMessage();
        if(GlobalCache.getIsautoProgram()){
            finish();
        }
//        mIvStart.setSelected(DataManager.getDataManager().isPause());
    }
}
